package org.basic.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.basic.common.core.utils.MapstructUtils;
import org.basic.common.core.utils.StringUtils;
import org.basic.common.mybatis.core.page.PageQuery;
import org.basic.common.mybatis.core.page.TableDataInfo;
import org.basic.sms.domain.SysSmsLog;
import org.basic.sms.domain.bo.SysSmsLogBo;
import org.basic.sms.domain.vo.SysSmsLogVo;
import org.basic.sms.mapper.SysSmsLogMapper;
import org.basic.sms.service.ISysSmsLogService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 邮件日志Service业务层处理
 *
 * @author Shao Hua
 * @date 2023-07-20
 */
@RequiredArgsConstructor
@Service
public class SysSmsLogServiceImpl implements ISysSmsLogService {

    private final SysSmsLogMapper baseMapper;

    /**
     * 查询邮件日志
     */
    @Override
    public SysSmsLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询邮件日志列表
     */
    @Override
    public TableDataInfo<SysSmsLogVo> queryPageList(SysSmsLogBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<SysSmsLog> lqw = buildQueryWrapper(bo);
        Page<SysSmsLogVo> result = baseMapper.selectListByPage(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询邮件日志列表
     */
    @Override
    public List<SysSmsLogVo> queryList(SysSmsLogBo bo) {
        LambdaQueryWrapper<SysSmsLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysSmsLog> buildQueryWrapper(SysSmsLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysSmsLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTemplateId() != null, SysSmsLog::getTemplateId, bo.getTemplateId());
        lqw.eq(bo.getPhone() != null, SysSmsLog::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), SysSmsLog::getContent, bo.getContent());
        lqw.eq(bo.getSendStatus() != null, SysSmsLog::getSendStatus, bo.getSendStatus());
        lqw.eq(bo.getReceiveStatus() != null, SysSmsLog::getReceiveStatus, bo.getReceiveStatus());
        return lqw;
    }

    /**
     * 新增邮件日志
     */
    @Override
    public Boolean insertByBo(SysSmsLogBo bo) {
        SysSmsLog add = MapstructUtils.convert(bo, SysSmsLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改邮件日志
     */
    @Override
    public Boolean updateByBo(SysSmsLogBo bo) {
        SysSmsLog update = MapstructUtils.convert(bo, SysSmsLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysSmsLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除邮件日志
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
